package com.vend.common.glide

import android.content.Context
import com.bumptech.glide.GlideBuilder
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.load.engine.bitmap_recycle.LruArrayPool
import com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool
import com.bumptech.glide.load.engine.cache.LruResourceCache
import com.bumptech.glide.load.engine.cache.MemorySizeCalculator
import com.bumptech.glide.module.AppGlideModule
import com.bumptech.glide.request.RequestOptions
import com.vend.common.VendBaseApplication

/**
 * Created by glendal on 2022/01/13.
 * 制定glideModel
 * 参考： http://events.jianshu.io/p/448c840f583c
 */
@GlideModule
class GModel : AppGlideModule() {
    override fun applyOptions(context: Context, builder: GlideBuilder) {
        val calculator: MemorySizeCalculator = MemorySizeCalculator.Builder(VendBaseApplication.instance).build()
        val defaultMemoryCacheSize: Int = calculator.getMemoryCacheSize()
        val defaultBitmapPoolSize: Int = calculator.getBitmapPoolSize()
        val defaultArrayPoolSize: Int = calculator.getArrayPoolSizeInBytes()
        builder.setDefaultRequestOptions(
            RequestOptions()
                .format(DecodeFormat.PREFER_RGB_565)
        )
        builder.setMemoryCache(LruResourceCache((defaultMemoryCacheSize / 2).toLong()))
        builder.setBitmapPool(LruBitmapPool((defaultBitmapPoolSize / 2).toLong()))
        builder.setArrayPool(LruArrayPool(defaultArrayPoolSize / 2))
    }
}